package com.example.demo4.service.impl;

import com.example.demo4.entity.Fruit;
import com.example.demo4.service.AbstractStrategy;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
/**
 * 满减
 */
public class PriceFullDecrement extends AbstractStrategy {
    @Override
    public  BigDecimal acceptCash(List<Fruit> fruits){
        BigDecimal totalPrice = new BigDecimal(0) ;
        if(!CollectionUtils.isEmpty(fruits)){
            for(Fruit f : fruits){
                BigDecimal weight = f.getWeight();
                BigDecimal price = f.getPrice();
                BigDecimal discount = f.getDiscount().multiply(new BigDecimal(0.1));
                totalPrice  =totalPrice.add(price.multiply(weight).multiply(discount));
            }
        }
        if(totalPrice.compareTo(full)>=0){
            totalPrice=totalPrice.subtract(decrement);
        }
        return totalPrice.setScale(2,BigDecimal.ROUND_HALF_UP);
    }


    private BigDecimal full =new BigDecimal(0);
    private BigDecimal decrement =new BigDecimal(0);

    public PriceFullDecrement(BigDecimal full, BigDecimal decrement) {
        this.full = full;
        this.decrement = decrement;
    }

}